/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-08-11
 * Time: 20:55
 */
public class Test {
    //leetcode 977 有序数组的平方
    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        int[] ret = new int[nums.length];
        int index = nums.length-1;
        while(left <= right) {
            if(Math.abs(nums[left]) > Math.abs(nums[right])) {
                ret[index--] = nums[left] * nums[left];
                left++;
            }else {
                ret[index--] = nums[right] * nums[right];
                right--;
            }
        }

        return ret;
    }

    public int[][] generateMatrix(int n) {
        int[][] ret = new int[n][n];
        int x = 0;
        int y = 0;
        Boolean flg1 = true; //x方向
        Boolean flg2 = true; //y方向
        int lim1 = 0; //x左边界
        int lim2 = n-1; //x有边界
        int lim3 = 0; //y上边界
        int lim4 = n-1; //y下边界
        for(int i = 1; i <= n * n; i++) {
            if(flg1 && flg2) {
                ret[x][y] = i;
                if(y != lim2) {
                    y++;
                }
            }else if(!flg1 && flg2) {
                ret[x][y] = i;
                if(x != lim4) {
                    x++;
                }
            }else if(!flg1 && !flg2) {
                ret[x][y] = i;
                if(y != lim1) {
                    y--;
                }
            }else {
                ret[x][y] = i;
                if(x != lim1) {
                    x--;
                }
            }
            if(flg1 && y == lim2) {
                flg1 = false;
                lim3++;
                lim2--;
            }
            if(flg2 && x == lim4) {
                flg2 = false;
                lim4--;
            }
            if(!flg1 && y == lim1) {
                flg1 = true;
                lim1++;
            }
            if(!flg2 && x == lim3) {
                flg2 = true;
                lim3++;
            }
        }

        return ret;
    }
}
